# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json
import codecs


class ArticlespiderPipeline(object):
    def process_item(self, item, spider):
        return item

class SinaPipline(object):
    def process_item(self,item,spider):
        Head = item["Head"]
        filename = 'G:\python\scrapy\sina'
        filename = filename + '\\' + Head + '.txt'
        if item['Content'] != '':
            fp = open(filename,'W',encoding="utf-8")
            fp.write(item['Content'])
            fp.close()
        return item

class JsonWithEncodingPipline(object):
    def process_item(self,item,spider):
        title = item['title']
        file_name = 'G:\python\scrapy\sina'
        file_name = file_name + '\\' + title + '.json'

        self.file = codecs.open(file_name,'w',encoding="utf-8")
        lines = json.dumps(dict(item),ensure_ascii=False) + "\n"
        self.file.write(lines)
        print("插入"+title+"成功")
        return item